package com.lhy.controller;

import com.lhy.domain.Department;
import com.lhy.domain.User;
import com.lhy.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 汪
 */
@Controller
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @RequestMapping("/showAllDepartment")
    public String showAllDepartment(HttpServletRequest req, HttpServletResponse resp) {
        List<Department> departments = departmentService.showAllDepartment();
        HttpSession session = req.getSession();
        session.setAttribute("departments", departments);
        return "kslist";
    }

    @RequestMapping("/findSomeDepartment")
    public String findSomeDepartment(HttpServletRequest req, HttpServletResponse resp) {
        Integer departId = null;
        try {
            departId = Integer.parseInt(req.getParameter("departid"));
        } catch (Exception e) {
        }
        String name = req.getParameter("name");
        Department department = new Department();
        department.setDepartId(departId);
        department.setName(name);
        List<Department> departments = departmentService.findSomeDepartment(department);
        HttpSession session = req.getSession();
        session.setAttribute("departments", departments);
        return "kslist";

    }
    @RequestMapping("/addDepartment")
    public String addDepartment(HttpServletRequest req, HttpServletResponse resp){
        Department department=new Department();
        Integer departId=Integer.parseInt(req.getParameter("departId"));
        String name = req.getParameter("name");
        Date date=new Date();
        HttpSession session = req.getSession();
        Integer userId = ((User) session.getAttribute("user")).getId();
        department.setDepartId(departId);
        department.setUserId(userId);
        department.setCreateDate(date);
        department.setName(name);
        departmentService.addDepartment(department);
        return "main";
    }
    @RequestMapping("/deleteDepartment")
    public String deleteDepartment(HttpServletRequest req, HttpServletResponse resp){
        Integer departId=Integer.parseInt(req.getParameter("departid"));
        departmentService.deleteDepartment(departId);
        return "main";
    }
    @RequestMapping("/getDepartmentId")
    public String getDepartmentId(HttpServletRequest req, HttpServletResponse resp){
        Integer departId=Integer.parseInt(req.getParameter("departid"));
        HttpSession session = req.getSession();
        session.setAttribute("id",departId);
        return "ksmodify";
    }
    @RequestMapping("/modifyDepartment")
    public String modifyDepartment(HttpServletRequest req, HttpServletResponse resp){
        Integer old_departid=Integer.parseInt(req.getParameter("old_departid"));
        Integer departId=Integer.parseInt(req.getParameter("departid"));
        String name = req.getParameter("name");
        departmentService.modifyDepartment(departId,name,old_departid);
        return "main";
    }
}
